import {
  getUserAgree,
  signAgreement,
  getUserInfo
} from "../../services/_utils/api"
import {
  GETSTORAGE
} from "../../services/storage/storage"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    familyArchiveStatus: '',
    currentAgreementVersion: '',
    signedAgreementVersion: '',
    userId: '',
    agreementId: '',
    contentFlag: false,
    familyMemberId: '',
    inviteStaus: false,
    btnFlag: false
  },
  // 获取用户当前信息
  userInfo() {
    const userId = GETSTORAGE('userId')
    console.log(userId, '本地');
    getUserInfo({
      wechatMiniprogramOpenId: '',
      userId: userId,
      wechatServiceOfficialAccountOpenId: '',
      wechatUnionId: ''
    }).then(res => {
      console.log(res, "获取用户当前信息");
      const {
        currentAgreementVersion,
        signedAgreementVersion,
        familyArchiveStatus
      } = res.data.data
      if (currentAgreementVersion == signedAgreementVersion) {
        if (this.data.inviteStaus == true) {
          console.log(1);
          wx.navigateTo({
            url: `/pages/filing/inviteViewPlus/inviteViewPlus?familyMemberId=${this.data.familyMemberId}`,
          })
          return
        } else if (familyArchiveStatus == "empty") {
          wx.navigateTo({
            url: '/pages/filing/filing',
          })
        } else if (familyArchiveStatus == "member") {
          wx.navigateTo({
            url: '/pages/filing/family/family',
          })
          return
        } else if (familyArchiveStatus == "health_manage") {
          wx.navigateTo({
            url: '/pages/filing/health-management/healt-hmanagement',
          })
          return
        } else {
          wx.switchTab({
            url: '/pages/index/index',
          })
          return
        }
        this.setData({
          contentFlag: false,
          currentAgreementVersion,
          signedAgreementVersion,
          familyArchiveStatus
        })
      } else {
        this.setData({
          contentFlag: true,
          currentAgreementVersion,
          signedAgreementVersion,
          familyArchiveStatus
        })
        // 调用获取版本协议
        this.signAgreement()
      }
    })
  },
  signAgreement() {
    // 根据版本号获取用户协议
    getUserAgree({
      // version: currentAgreementVersion
      version: this.data.currentAgreementVersion
    }).then(res => {
      console.log("获取用户协议res", res);
      const content = res.data.data?.content
      const agreementId = res.data.data?.agreementId
      this.setData({
        content,
        agreementId
      })
    })
  },
  // 点击同意
  handlePopup() {
    if (this.data.btnFlag == false) return
    const userId = GETSTORAGE('userId')
    let obj = {
      userId: userId,
      agreementId: this.data.agreementId
    }
    // 签署协议
    signAgreement(obj).then(res => {
      console.log("签署协议res", res);
      console.log("邀请进的", this.data.inviteStaus);
      if (this.data.inviteStaus == true) {
        console.log(1);
        wx.navigateTo({
          url: `/pages/filing/inviteViewPlus/inviteViewPlus?familyMemberId=${this.data.familyMemberId}`,
        })
        return
      } else if (this.data.familyArchiveStatus == "empty") {
        console.log(2);
        wx.navigateTo({
          url: '/pages/filing/filing',
        })
        return
      } else if (this.data.familyArchiveStatus == "member") {
        console.log(3);
        wx.navigateTo({
          url: '/pages/filing/family/family',
        })
        return
      } else if (this.data.familyArchiveStatus == "health_manage") {
        console.log(4);
        wx.navigateTo({
          url: '/pages/filing/health-management/healt-hmanagement',
        })
      } else {
        console.log(5);
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
    this.setData({
      contentFlag: false
    })
    console.log(this.data.contentFlag);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, 'xinxi');
    if (options.invite == 'invite') {
      this.setData({
        inviteStaus: true,
        familyMemberId: options.familyMemberId
      })
    } else {
      this.setData({
        inviteStaus: false
      })
    }
    this.userInfo()
  },

  handleRoll(e) {
    console.log(e);
    this.setData({
      btnFlag: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})